<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php



class MSecurityClass {
                public $expire = 0;

    
    function verification($list,$expire_time,$signature) {
        if (!is_array($list)) {
            FXP::log(FXP::t("list is not list"), MLogger::LEVEL_ERROR);
            return false;
        }
        $this->expire = (int)$expire_time;
        if ($this->virify_timestamp() == false) {
            FXP::log(FXP::t("virify_timestamp verification error"), MLogger::LEVEL_ERROR);
            return  false;
        }
        
        $retval = $this->encrypt_param($list);
        if ($retval != $signature) { 
            FXP::log(FXP::t("signature is not same, retval:'{$retval}', signature:'{$signature}'"), MLogger::LEVEL_ERROR);
            return false;
        }
        return true;
    }

    
    function virify_timestamp() {
        $time_now = time();
        if ($time_now > $this->expire) {
            return false;
        }
        return true;
    }

    
    function encrypt_param($input=array()) {
        $retval = null;
        if (!is_array($input)) {
            return null;
        }
        $list = array_keys($input);
        $retval = $this->sort_and_assemble($list, $input);
        $retval = $this->assemble_timestamp($retval);
        $retval = $this->get_signature($retval);
        return $retval;
    }

    
    function assemble_timestamp($input) {
        return $input . EXPIRATION_DATE . $this->expire;
    }

    
    function sort_and_assemble($list,$map) {
        $retval = "";
        natcasesort($list);
        foreach ($list as $key) {
            $retval = $retval . $key . $map[$key];
        }
        return $retval;
    }

    
    function get_signature($input) {
        $retval = sha1($input . MConst::ACCESS_KEY);
        return $retval;
    }

}
?>
